package com.siyoumi.app.modules.app_ess_qr.service;

import com.siyoumi.app.entity.SysAbc;
import com.siyoumi.app.modules.app_ess_qr.vo.VoEssQr;
import com.siyoumi.app.modules.app_fks.vo.VaFksCity;
import com.siyoumi.app.service.SysAbcService;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XSpringContext;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.config.SysConfig;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.service.IWebService;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//城市
@Slf4j
@Service
public class SvcEssQr
        implements IWebService {
    static public SvcEssQr getBean() {
        return XSpringContext.getBean(SvcEssQr.class);
    }

    static public SysAbcService getApp() {
        return SysAbcService.getBean();
    }

    public JoinWrapperPlus<SysAbc> listQuery() {
        return listQuery(InputData.getIns());
    }

    public String getQrUrl(SysAbc entity) {
        String txt = "https://www.baidu.com";
        return XStr.format("{0}sys/app/api/qr?txt={1}"
                , SysConfig.getIns().getAppRoot()
                , XStr.urlEnc(txt)
        );
    }

    public String getAbcTable() {
        return "app_ess_qr";
    }

    /**
     * select
     *
     * @return query
     */
    public JoinWrapperPlus<SysAbc> listQuery(InputData inputData) {
        String name = inputData.input("name");

        JoinWrapperPlus<SysAbc> query = getApp().join();
        query.eq("abc_x_id", XHttpContext.getX())
                .eq("abc_table", getAbcTable())
                .eq("abc_del", 0);
        //排序
        query.orderByAsc("abc_order")
                .orderByDesc("abc_id");
        if (XStr.hasAnyText(name)) { //名称
            query.like("abc_name", name);
        }

        return query;
    }

    public XReturn edit(InputData inputData, VoEssQr vo) {
        List<String> ignoreField = new ArrayList<>();
        if (inputData.isAdminEdit()) {
        }

        return XApp.getTransaction().execute(status -> {
            XReturn r = getApp().saveEntity(inputData, vo, false, ignoreField);
            return r;
        });
    }

    /**
     * 删除
     */
    @SneakyThrows
    @Transactional(propagation = Propagation.MANDATORY)
    public XReturn delete(List<String> ids) {
        XReturn r = XReturn.getR(0);

        getApp().delete(ids);

        return r;
    }
}
